package com.jingdianjichi.interview.server.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingdianjichi.interview.api.common.PageInfo;
import com.jingdianjichi.interview.api.common.PageResult;
import com.jingdianjichi.interview.api.enums.IsDeletedFlagEnum;
import com.jingdianjichi.interview.api.req.GetShareMomentReq;
import com.jingdianjichi.interview.api.req.RemoveShareMomentReq;
import com.jingdianjichi.interview.api.req.SaveMomentCircleReq;
import com.jingdianjichi.interview.api.vo.ShareMomentVO;
import com.jingdianjichi.interview.server.entity.po.ShareMoment;
import com.jingdianjichi.interview.server.mapper.ShareMomentMapper;
import com.jingdianjichi.interview.server.rpc.UserRpc;
import com.jingdianjichi.interview.server.service.ShareMomentService;
import com.jingdianjichi.interview.server.utils.LoginUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * ClassName:ShareMomentServiceImpl
 * Package:com.jingdianjichi.circle.server.service.Impl
 * Description:
 *
 * @Author fst
 * @Create 2025/2/17 20:53
 * @Version 1.0
 */

@Service
public class ShareMomentServiceImpl extends ServiceImpl<ShareMomentMapper, ShareMoment> implements ShareMomentService
{

    @Resource
    private UserRpc userRpc;
    @Override
    public Boolean saveMoment(SaveMomentCircleReq req) {

        ShareMoment moment=new ShareMoment();

        moment.setCircleId(req.getCircleId());
        moment.setContent(req.getContent());

        if(Objects.nonNull(req.getPicUrlList()))
        {
            moment.setPicUrls(JSON.toJSONString(req.getPicUrlList()));
        }

        moment.setReplyCount(0);
        moment.setCreatedBy(LoginUtil.getLoginId());
        moment.setCreatedTime(new Date());
        moment.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        return super.save(moment);
    }

    @Override
    public PageResult<ShareMomentVO> getMoments(GetShareMomentReq req) {
        LambdaQueryWrapper<ShareMoment> query = Wrappers.<ShareMoment>lambdaQuery()
                .eq(Objects.nonNull(req.getCircleId()), ShareMoment::getCircleId, req.getCircleId())
                .eq(ShareMoment::getIsDeleted, IsDeletedFlagEnum.UN_DELETED.getCode())
                .orderByDesc(ShareMoment::getCreatedTime);
        PageInfo pageInfo = req.getPageInfo();
        Page<ShareMoment> page = new Page<>(pageInfo.getPageNo(), pageInfo.getPageSize());
        Page<ShareMoment> pageRes = super.page(page, query);
        PageResult<ShareMomentVO> result = new PageResult<>();
        List<ShareMoment> records = pageRes.getRecords();
//        List<String> userNameList = records.stream().map(ShareMoment::getCreatedBy).distinct().collect(Collectors.toList());
//        Map<String, UserInfo> userInfoMap = userRpc.batchGetUserInfo(userNameList);
//        UserInfo defaultUser = new UserInfo();
        List<ShareMomentVO> list = records.stream().map(item -> {
            ShareMomentVO vo = new ShareMomentVO();
            vo.setId(item.getId());
            vo.setCircleId(item.getCircleId());
            vo.setContent(item.getContent());
            if (Objects.nonNull(item.getPicUrls())) {
                List<String> picList = JSONArray.parseArray(item.getPicUrls(), String.class);
                vo.setPicUrlList(picList);
            }
            vo.setReplyCount(item.getReplyCount());
            vo.setCreatedTime(item.getCreatedTime().getTime());
           // UserInfo user = userInfoMap.getOrDefault(item.getCreatedBy(), defaultUser);
            //vo.setUserName(user.getNickName());
            //vo.setUserAvatar(user.getAvatar());
            return vo;
        }).collect(Collectors.toList());
        result.setRecords(list);
        result.setTotal((int) pageRes.getTotal());
        result.setPageSize(pageInfo.getPageSize());
        result.setPageNo(pageInfo.getPageNo());
        return result;

    }

    @Override
    public Boolean removeMoment(RemoveShareMomentReq req)
    {
        return super.update(Wrappers.<ShareMoment>lambdaUpdate().eq(ShareMoment::getId, req.getId())
                .eq(ShareMoment::getIsDeleted, IsDeletedFlagEnum.UN_DELETED.getCode()).
                set(ShareMoment::getIsDeleted, IsDeletedFlagEnum.DELETED.getCode())) ;
    }
}
